import binascii
import os 
# following imports are required by PKI
import Crypto
import Crypto.Random
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
import hashlib
import configparser #处理ini文件
import datetime
class Student:
    Name = ''
    Credits = 20.0
    _private_key = ''
    _public_key = ''
    def __init__(self,stuName):
        PrivateFile = "%s_Private.pem" % (stuName)
        if os.path.exists(PrivateFile):
            #导入已有账户的私钥
            with open(PrivateFile, "rb") as f:
                privatePemStr = f.read()
                self._private_key = Crypto.PublicKey.RSA.importKey(privatePemStr)
        else:
            #创建新的账户
            random = Crypto.Random.new().read
            self._private_key = RSA.generate(1024, random)
            # 保存密钥
            with open(PrivateFile, "wb") as f:
                privatePemStr = self._private_key.exportKey()   # 导出私钥
                f.write(privatePemStr)
                f.close()

        publickFile = "%s_Public.pem" % (stuName)
        if os.path.exists(publickFile):
            #导入已有账户的公钥
            with open(publickFile, "rb") as f:
                publicPemStr = f.read()
                self._public_key = Crypto.PublicKey.RSA.importKey(publicPemStr)
        else:
            self._public_key = self._private_key.publickey()
            publicPemStr = self._public_key.exportKey()   # 导出公钥
            with open(publickFile, "wb") as f:
                f.write(publicPemStr)
        #计算出账户
        str1 = binascii.hexlify(self._public_key.exportKey(format='DER')).decode('ascii')
        str2 = hashlib.sha256(str1.encode('utf-8')).hexdigest()
        str3 = hashlib.sha256(str2.encode('utf-8')).hexdigest()        
        self.Name = str3
        print("Account is ready: %s" % (self.Name))

class Transaction:
    iniFileName = "Account.ini"
    def __init__(self):
       return

    def Transfer(self, sender, recipient, value):
        sender.Credits = self.AccountGet(sender)
        recipient.Credits = self.AccountGet(recipient)
        if sender.Credits > value:
           sender.Credits -= value
           recipient.Credits += value
           self.time = datetime.datetime.now()
           print('Transaction finished at %s' % (self.time))
        self.AccountSave(sender)
        self.AccountSave(recipient)
        return 

    def AccountGet(self, Stu): #存储账户余额信息
        cred = Stu.Credits
        config = configparser.ConfigParser()
        config.read(self.iniFileName, encoding='utf-8')
        list = config.sections()    # 获取到配置文件中所有分组名称
        if Stu.Name in list: # 如果分组存在
            cred = config.getfloat(Stu.Name, "Credits")
        return cred

    def AccountSave(self, Stu): #存储账户余额信息
        config = configparser.ConfigParser()
        config.read(self.iniFileName, encoding='utf-8')
        list = config.sections()    # 获取到配置文件中所有分组名称
        if Stu.Name not in list: # 如果分组不存在
            config.add_section(Stu.Name)   # 增加section
        config.set(Stu.Name,"Credits",str(Stu.Credits))
        with open(self.iniFileName, "w+") as f:
            config.write(f)
            f.close()
        return

StuA = Student('A')
StuB = Student('B')
tran = Transaction()
tran.Transfer(StuA,StuB,1.1)

print('Student: %s has %f credits' % (StuA.Name,StuA.Credits))
print('Student: %s has %f credits' % (StuB.Name,StuB.Credits))